var u = require("../../api.js"),
  t = u,
  h = u,
  rr = require("../../utils.js"),
  a = getApp(),
  j = a,
  o = a;

Page({

  /**
   * 页面的初始数据
   */
  data: {
      huodong:'',
      goods_list:[],
      page:1,
      is_ok:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (t) {
    if(!t.id>0) {
      wx.showToast({
        title: '非法入口，请重新进入',
        icon:"none"
      })
    }else {
      a.pageOnLoad(this, t);
      this.onloadMore(t);
    }
  },
  onloadMore: function (t) {
    var that = this;
    a.request({
      url: u.activity.info,
      data: {
        id:t.id
      },
      method: "POST",
      success: function (res) {
        if (res.code == 0) {
          that.setData({
            huodong:res.info
          })
          wx.setNavigationBarTitle({
            title: res.info.title,
          })
          that.getGoodsList(t);
        }else {
          wx.showToast({
            title: res.msg,
            icon:'none'
          })
        }
      }
    })
  },
  getGoodsList: function (t) {
    var that = this;
    var id = that.data.huodong != '' ? that.data.huodong.id : t.id;
    wx.showLoading({
      title: '加载中',
    })
    a.request({
      url: u.activity.goods_list,
      data: {
        id: id,
        page:that.data.page
      },
      method: "POST",
      success: function (res) {
        if (res.code == 0 && res.data.list.length) {
            var goods_list = that.data.goods_list;
            goods_list = goods_list.concat(res.data.list)
            console.log(goods_list);
            that.setData({
              goods_list:goods_list,
              page: that.data.page+1
            })
        }else {
          that.setData({
            is_ok:1
          })
        }
      },
      complete: function(res) {
        wx.hideLoading();
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    a.pageOnShow(this);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.is_ok==0) {
      this.getGoodsList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: this.data.huodong.title,  
    }
  },
  hideAttrPicker: function () {
    this.setData({
      show_attr_picker: false
    });
  },

  numberSub: function () {
    var goods_num = this.data.goods_num;
    if (goods_num > 1) {
      goods_num--;
    }
    var minusStatus = goods_num <= 1 ? 'disabled' : 'normal';
    this.setData({
      goods_num: goods_num,
      minusStatus: minusStatus
    });
  },
  numberAdd: function () {
    var goods_num = this.data.goods_num;
    goods_num++;
    var minusStatus = goods_num < 1 ? 'disabled' : 'normal';
    this.setData({
      goods_num: goods_num,
      minusStatus: minusStatus
    });
  },


  attrGoodsClick: function (a) {
    var i = this,
      r = a.target.dataset.groupId,
      e = a.target.dataset.id,
      s = i.data.attr_group_list;
    for (var d in s)
      if (s[d].attr_group_id == r)
        for (var n in s[d].attr_list) s[d].attr_list[n].attr_id == e ? s[d].attr_list[n].checked = !0 : s[d].attr_list[n].checked = !1;
    i.setData({
      attr_group_list: s
    });
    var c = [],
      u = !0;
    for (var d in s) {
      var _ = !1;
      for (var n in s[d].attr_list)
        if (s[d].attr_list[n].checked) {
          c.push(s[d].attr_list[n].attr_id), _ = !0;
          break;
        }
      if (!_) {
        u = !1;
        break;
      }
    }
    u && (wx.showLoading({
      title: "正在加载",
      mask: !0
    }), j.request({
      url: t.default.goods_attr_info,
      data: {
        goods_id: i.data.goods_info.id,
        attr_list: JSON.stringify(c)
      },
      success: function (t) {
        if (wx.hideLoading(), 0 == t.code) {
          var a = i.data.goods_info;
          a.price = t.data.price, a.num = t.data.num, a.attr_pic = t.data.pic, i.setData({
            goods_info: a
            // goods :a
          });
        }
      }
    }));
  },
  // 加入购物车
  addToCart: function () {
    this.submit('ADD_CART')
  },


  submit: function (a) {
    var i = this;
    // if (!i.data.show_attr_picker) return i.setData({
    //     show_attr_picker: !0
    // }), !0;
    if (i.data.miaosha_data && i.data.miaosha_data.rest_num > 0 && i.data.form.number > i.data.miaosha_data.rest_num) return wx.showToast({
      title: "商品库存不足，请选择其它规格或数量",
      image: "/images/icon-warning.png"
    }), !0;
    if (i.data.goods_num > i.data.goods_info.num) return wx.showToast({
      title: "商品库存不足，请选择其它规格或数量",
      image: "/images/icon-warning.png"
    }), !0;
    var r = i.data.attr_group_list,
      e = [];
    for (var s in r) {
      var d = !1;
      for (var n in r[s].attr_list)
        if (r[s].attr_list[n].checked) {
          d = {
            attr_id: r[s].attr_list[n].attr_id,
            attr_name: r[s].attr_list[n].attr_name
          };
          break;
        }
      if (!d) return wx.showToast({
        title: "请选择" + r[s].attr_group_name,
        image: "/images/icon-warning.png"
      }), !0;
      e.push({
        attr_group_id: r[s].attr_group_id,
        attr_group_name: r[s].attr_group_name,
        attr_id: d.attr_id,
        attr_name: d.attr_name
      });
    }
    if ("ADD_CART" == a && (wx.showLoading({
      title: "正在提交",
      mask: !0
    }), j.request({
      url: t.cart.add_cart,
      method: "POST",
      data: {
        goods_id: i.data.goods_info.id,
        attr: JSON.stringify(e),
        num: i.data.goods_num
      },
      success: function (t) {
        wx.hideLoading(), wx.showToast({
          title: t.msg,
          duration: 1500
        }), i.setData({
          show_attr_picker: !1
        });
        if (!(i.data.goods_cart_index === false)) {
        }
      },
      fail: function (res) {
        wx.hideLoading()
      },
      complete: function (res) {
        wx.hideLoading()
      }

    })), "BUY_NOW" == a) {
      i.setData({
        show_attr_picker: !1
      });
      var c = [];
      c.push({
        goods_id: i.data.id,
        num: i.data.form.number,
        attr: e
      });
      var u = i.data.goods,
        _ = 0;
      null != u.mch && (_ = u.mch.id);
      var g = [];
      g.push({
        mch_id: _,
        goods_list: c
      }), wx.redirectTo({
        url: "/pages/new-order-submit/new-order-submit?mch_list=" + JSON.stringify(g)
      });
    }
  },

})